//
//  LYSubCategotyViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYSubCategotyViewController.h"
#import "LYClassWebViewController.h"
#import "LYEArtistCell.h"
#import "LYVideoModel.h"
#import "LYCollectListModel.h"

@interface LYSubCategotyViewController ()

@property (weak, nonatomic) IBOutlet VVYRefreshTableView *tableView;
@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, assign) NSInteger page;

@end

@implementation LYSubCategotyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    LYGetSubjectParameterModel *requestModel = [self getRequsetModelWithPage:@"1"];
    
    [LYRequestTool getSubjectListWithParmeterModel:requestModel success:^(NSArray<LYMyClassListModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
    
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
    
    LYGetSubjectParameterModel *requestModel = [self getRequsetModelWithPage:page];
    
    [LYRequestTool getSubjectListWithParmeterModel:requestModel success:^(NSArray<LYMyClassListModel *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}







#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYMyClassListModel *model = self.models[indexPath.row];
    LYClassWebViewController *classWebVC = [LYClassWebViewController classWebVCWithID:model.Id];
    [self.navigationController pushViewController:classWebVC animated:YES];
    
}


#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYEArtistCell";
    LYEArtistCell *cell = [LYEArtistCell cellWithTableView:tableView andIndetifier:cellID];
    
    //model
    LYMyClassListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
    
}


#pragma mark - 其他

- (LYGetSubjectParameterModel *)getRequsetModelWithPage:(NSString *)page{

    LYGetSubjectParameterModel *model = [[LYGetSubjectParameterModel alloc] init];
    model.page = page;
    model.per_page = @"10";
    model.token = LYToken;
    model.subject_path = self.subject_id;
    return model;
    
}


@end
